package cn.wolfcode.web.controller;


import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.SystemQueryObject;
import cn.wolfcode.service.ISystemDictionaryItemService;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import cn.wolfcode.util.PermissionAnnotation;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/systemDictionaryItem")
public class SystemDictionaryItemController {

    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;

    @Autowired
    private ISystemDictionaryService systemDictionaryService;


    @PermissionAnnotation(name="字典明细页面",expression="systemDictionaryItem:list")
    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") SystemQueryObject qo){

        PageInfo<SystemDictionaryItem> pageInfo = systemDictionaryItemService.query(qo);
        model.addAttribute("PageInfo", pageInfo);
        model.addAttribute("Dictionary",systemDictionaryService.listAll());
        return "systemDictionaryItem/list";
    }


    @PermissionAnnotation(name="字典明细删除",expression="systemDictionaryItem:delete")
    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           systemDictionaryItemService.delete(id);
        }
        return "redirect:/systemDictionaryItem/list";
    }


    @PermissionAnnotation(name="字典明细新增/编辑",expression="systemDictionaryItem:saveOrUpdate")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(SystemDictionaryItem systemDictionaryItem) {
        if (systemDictionaryItem.getId() == null) {
            if (systemDictionaryItem.getTypeId()==null && systemDictionaryItem.getSys().getId()==null){
               return "redirect:/systemDictionaryItem/list";
            }
            if (systemDictionaryItem.getSys().getId()!=null){
                systemDictionaryItem.setTypeId(null);
            }
            systemDictionaryItemService.save(systemDictionaryItem);
        } else {
            systemDictionaryItemService.update(systemDictionaryItem);
        }
        return "redirect:/systemDictionaryItem/list";
    }



    @RequestMapping("/getSystem")
    @ResponseBody
    public List<SystemDictionaryItem> getSystem(Long id){
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.selectById(id);
        return systemDictionaryItems;
    }

    @RequestMapping("/selectByParentId")
    @ResponseBody
    public List<SystemDictionaryItem> selectByParentId(Long parentId){
        List<SystemDictionaryItem> systemDictionaryItems = systemDictionaryItemService.selectByParentId(parentId);
        return systemDictionaryItems;
    }
}
